package com.xbongbong.workflow.service.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.FilterCondition;
import com.xbongbong.workflow.pojo.WorkflowRulePojo;
import com.xbongbong.workflow.service.field.AbstractWorkflowConditionFieldService;
import com.xbongbong.workflow.service.field.WorkflowConditionFieldService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 工作流获取数据过滤条件阶段推进器字段处理
 * @Author sunshaoxiang
 * @Date 2021/12/6 15:04
 * @Version 1.0
 */
@Service("WorkflowConditionStageServiceImpl")
public class WorkflowConditionStageServiceImpl extends AbstractWorkflowConditionFieldService implements WorkflowConditionFieldService {

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.STAGE_THRUSTER.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, FilterCondition filterCondition, WorkflowRulePojo workflowRulePojo) throws XbbException {
        String corpid = paasFormDataEntity.getCorpid();
        Long formId = paasFormDataEntity.getFormId();
        Integer businessType = paasFormDataEntity.getBusinessType();
        Integer saasMark = paasFormDataEntity.getSaasMark();
        List<ItemPoJo> itemPoJos = proFormHelp.getEditStageComboItems(fieldAttrEntity, saasMark, businessType, corpid, formId, data);
        fieldAttrEntity.setItems(itemPoJos);
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(Object object, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, WorkflowRulePojo workflowRulePojo, FilterCondition filterCondition) throws XbbException {
        String corpid = workflowNodeEntity.getCorpid();
        Long formId = workflowNodeEntity.getFormId();
        Integer businessType = workflowNodeEntity.getBusinessType();
        Integer saasMark = workflowNodeEntity.getSaasMark();
        //这里获取数据根据获取启用流程的阶段信息去匹配
        List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
        targetField.setItems(itemPoJos);
        return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowNodeEntity workflowEntity, FilterCondition workflowConditionPojo) throws XbbException {
        Object object = workflowConditionPojo.getValue();
        String corpid = workflowEntity.getCorpid();
        Long formId = workflowEntity.getFormId();
        Integer saasMark = workflowEntity.getSaasMark();
        Integer businessType = workflowEntity.getBusinessType();
        List<ItemPoJo> itemPoJos = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
        targetField.setItems(itemPoJos);
        object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
        return object;
    }
}
